package org.everyday;

/**
 * @version v1.0.0
 * 创建时间：2022/7/15 10:09
 */
public class L558 {
    public Node intersect(Node quadTree1, Node quadTree2) {
        if (quadTree1.isLeaf){
            if (quadTree1.val){
                return new Node(true,true,null,null,null,null);
            }
            return quadTree2;
        }
        if (quadTree2.isLeaf){
            if (quadTree2.val){
                return new Node(true,true,null,null,null,null);
            }
            return quadTree1;
        }
        // 都不是叶子节点的情况
        Node bl = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft);
        Node br = intersect(quadTree1.bottomRight, quadTree2.bottomRight);
        Node tl = intersect(quadTree1.topLeft, quadTree2.topLeft);
        Node tr = intersect(quadTree1.topRight, quadTree2.topRight);
        if (bl.isLeaf && br.isLeaf && tl.isLeaf && tr.isLeaf && bl.val== br.val && br.val== tl.val && tl.val==tr.val){
            return new Node(bl.val,true,null,null,null,null);
        }
        return new Node(false, false, tl, tr, bl, br);
    }

    class Node {
        public boolean val;
        public boolean isLeaf;
        public Node topLeft;
        public Node topRight;
        public Node bottomLeft;
        public Node bottomRight;

        public Node() {}

        public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
            val = _val;
            isLeaf = _isLeaf;
            topLeft = _topLeft;
            topRight = _topRight;
            bottomLeft = _bottomLeft;
            bottomRight = _bottomRight;
        }
    }
}
